package com.startrek.ch02;

public class ArrayUtils {

    public static int[] moveForward(int[] a, int p) {
        if (p < 0 || p > a.length) {
            throw new IllegalArgumentException("p must be 0 < p < array_len!");
        }

        reverse(a, 0, p - 1);
        reverse(a, p, a.length - 1);
        reverse(a, 0, a.length - 1);

        return a;
    }

    private static void reverse(int[] a, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }

}
